package com.frank.http.expand;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

public class LocalEncrpUtils {
	private final static String TAG = LocalEncrpUtils.class.getSimpleName();
	private Cipher cipher = null;
	private Cipher decipher = null;
	public static final int ALGORIGHM_DES = 0;
	public static final int ALGORIGHM_AES = 1;
	private byte[] keyBytes = { -83, -5, 65, -57, 73, 44, 110, -93, 32, -88,
			123, 54, -49, -91, -44, 103, -64, 14, 116, 88, -33, -21, 67, -121,
			-29, 65, -60, 114, -62, 44, 74, -13 };
	private byte[] keyBytesDESede = { 88, -104, -113, -101, 84, -116, -80, -32,
			-101, 11, 121, 70, 8, 21, -98, -88, -32, -68, -71, 4, 69, 21, -29,
			-88 };
	static final String HEXES = "0123456789ABCDEF";

	public void init(int paramInt) {
		if (paramInt == 0)
			initDES();
		else
			initAES();
	}

	public void initDES() {
		try {
			SecretKeySpec localSecretKeySpec = new SecretKeySpec(
					this.keyBytesDESede, "DESede");
			IvParameterSpec localIvParameterSpec = new IvParameterSpec(
					new byte[8]);
			this.cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
			this.cipher.init(1, localSecretKeySpec, localIvParameterSpec);
			this.decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
			this.decipher.init(2, localSecretKeySpec, localIvParameterSpec);
			Logger.d(TAG, "initital for DES");
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	public void initAES() {
		try {
			this.cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			SecretKeySpec localSecretKeySpec = new SecretKeySpec(this.keyBytes,
					"AES");
			this.cipher.init(1, localSecretKeySpec);
			this.decipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			this.decipher.init(2, localSecretKeySpec);
			Logger.d(TAG, "initital for AES");
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	public String encryptBase64String(String paramString) throws Exception {
		byte[] arrayOfByte = encrypt(paramString);
		return new String(Base64.encode(arrayOfByte, 0)).trim();
	}

	public String decryptBase64String(String paramString) throws Exception {
		byte[] arrayOfByte1 = Base64.decode(paramString, 0);
		byte[] arrayOfByte2 = decrypt(arrayOfByte1);
		return new String(arrayOfByte2, "UTF-8").trim();
	}

	public byte[] encrypt(String paramString) throws Exception {
		byte[] arrayOfByte1 = paramString.getBytes("UTF-8");
		byte[] arrayOfByte2 = this.cipher.doFinal(arrayOfByte1);
		return arrayOfByte2;
	}

	public byte[] encrypt(byte[] paramArrayOfByte) throws Exception {
		return this.cipher.doFinal(paramArrayOfByte);
	}

	public byte[] decrypt(byte[] paramArrayOfByte) throws Exception {
		return this.decipher.doFinal(paramArrayOfByte);
	}

	public static String getHex(byte[] paramArrayOfByte) {
		if (paramArrayOfByte == null)
			return null;
		StringBuilder localStringBuilder = new StringBuilder(
				2 * paramArrayOfByte.length);
		byte[] arrayOfByte = paramArrayOfByte;
		int k = paramArrayOfByte.length;
		for (int j = 0; j < k; j++) {
			int i = arrayOfByte[j];
			localStringBuilder.append(
					"0123456789ABCDEF".charAt((i & 0xF0) >> 4)).append(
					"0123456789ABCDEF".charAt(i & 0xF));
		}
		return localStringBuilder.toString();
	}
}
